package com.bishe.tax.controller.sys;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bishe.tax.entity.InvoiceApply;
import com.bishe.tax.entity.SysUser;
import com.bishe.tax.service.InvoiceApplyService;
import com.bishe.tax.service.ToDoaskService;
import com.bishe.tax.utils.AjaxResult;
import com.bishe.tax.utils.TableDataInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 发票申请 信息操作处理
 *
 * @author ruoyi
 * @date 2019-06-23
 */
@Controller
@RequestMapping("/system/apply")
public class InvoiceApplyController {
	private String prefix = "invoiceApply";

	@Autowired
	private InvoiceApplyService applyService;
	@Autowired
	private ToDoaskService taskService;

	@GetMapping("list")
	public String apply() {
		return prefix + "/list";
	}

	/**
	 * 查询发票申请列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(InvoiceApply apply,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
		PageHelper.startPage(pageNum, pageSize);
		List<InvoiceApply> list = applyService.selectApplyList(apply);
		return TableDataInfo.returnDataTable(new PageInfo<>(list).getTotal(), list);
	}

	/**
	 * 新增发票申请
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存发票申请
	 */
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(InvoiceApply apply) {
		apply.setIaStatus(1);
		return AjaxResult.success(applyService.insertApply(apply));
	}

	/**
	 * 修改发票申请
	 */
	@GetMapping("/edit/{iaId}")
	public String edit(@PathVariable("iaId") Integer iaId,
			@RequestParam(value = "taskId", required = false) String taskId, ModelMap mmap) {
		InvoiceApply apply = applyService.selectApplyById(iaId);
		mmap.put("invoiceApply", apply);
		mmap.put("taskId", taskId);
		return prefix + "/edit";
	}

	/**
	 * 修改保存发票申请
	 */
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(InvoiceApply apply, @RequestParam(value = "taskId", required = false) String taskId) {
		if (StringUtils.isNotBlank(taskId)) {
			taskService.doComplete(taskId, null);
			apply.setIaStatus(2);
		}
		return AjaxResult.success(applyService.updateApply(apply));
	}

	/**
	 * 删除发票申请
	 */
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return AjaxResult.success(applyService.deleteApplyByIds(ids));
	}

	@GetMapping("examine/{taskId}/{iaId}")
	public String doExamine(@PathVariable("iaId") String iaId, @PathVariable("taskId") String taskId, ModelMap mmap) {
		InvoiceApply apply = applyService.selectApplyById(Integer.valueOf(iaId));
		mmap.put("invoiceApply", apply);
		mmap.put("taskId", taskId);
		return prefix + "/invoiceApply";
	}

	/**
	 * 审核
	 *
	 * @param taskId
	 * @param flag
	 */
	@PostMapping("complete")
	@ResponseBody
	public AjaxResult doComplete(InvoiceApply apply, @RequestParam("taskId") String taskId,
			@RequestParam("flag") Boolean flag) {
		taskService.doComplete(taskId, flag);
		if (flag) {
			apply.setIaStatus(4);
		} else {
			apply.setIaStatus(3);
		}
		return AjaxResult.success(applyService.updateApply(apply));
	}

	/**
	 * 提交审核
	 *
	 * @return
	 */
	@PostMapping("submitFlow")
	@ResponseBody
	public AjaxResult submitFlow(@RequestParam("iaId") Integer iaId, HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("sysUser");
		return AjaxResult.success(applyService.doSubmitFlow(user, iaId.toString()));
	}

}
